//
//  WBHomeViewController.swift
//  传智微博
//
//  Created by zhoujiming on 2016/12/15.
//  Copyright © 2016年 gmcc. All rights reserved.
//

import UIKit

// 原创微博可重用cell id
private let originalCellId = "originalCellId"
// 被转发微博可重用cell id
private let retweetedCellId = "retweetedCellId"

class WBHomeViewController: WBBaseViewController {
    
    @objc fileprivate func showFriends()
    {
        print(#function)
        
        let vc = WBDemoViewController()
        
        // vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
    // 列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    
    
    override func loadData() {
        
        print("准备刷新，最后一条：\(self.listViewModel.statusList.last?.status.text)")
        
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            
            print("加载数据结束");
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            if shouldRefresh {
                self.tableView?.reloadData()
            }
            
        }
        
    }

}

extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 取出视图模型，根据视图模型判断可重用cell
        var vm = listViewModel.statusList[indexPath.row]
        
        let cellId = vm.status.retweeted_status != nil ? retweetedCellId : originalCellId
        
        // 取cell
        // FIXME: 修改id
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        
        // 设置cell
        cell.viewModel = vm
        
        return cell
    }
    
    // 父类必须实现,子类才能重写
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // 根据indexpath获取视图模型
        let vm = listViewModel.statusList[indexPath.row]
        
        // 返回计算好的行高
        return vm.rowHeight
    }
}

extension WBHomeViewController{
    // 重写父类方法
    override func setupTableView() {
        super.setupTableView()
        
        
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        
        //        let btn : UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor:  UIColor.darkGray, highlightedColor: UIColor.orange)
        //
        //        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
        //
        //        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        // 注册原型cell
        //tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        
        tableView?.register(UINib.init(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        
        tableView?.register(UINib.init(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        // 设置行高
        // 取消自动行高
        //tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        // 取消分割线
        tableView?.separatorStyle = .none
        
        setupNaviTitle()
    }
    
    // 设置导航栏标题
    fileprivate func setupNaviTitle(){
        let title = WBNetworkManager.shared().userAccount.screen_name
        
        let btn = WBTitleButton(title: title)
        
        navItem.titleView = btn
        
        btn.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn: UIButton){
        btn.isSelected  = !btn.isSelected
    }
}
